package com.papers.juan.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.papers.common.excel.ExcelListener;
import com.papers.common.excel.ExcelResult;
import com.papers.common.utils.spring.SpringUtils;
import com.papers.juan.domain.vo.TJuanQuestionVo;
import com.papers.juan.service.ITJuanQuestionService;

import java.util.ArrayList;
import java.util.List;

public class TJunQuestionListener extends AnalysisEventListener<TJuanQuestionVo> implements ExcelListener<TJuanQuestionVo> {

    private final ITJuanQuestionService itJuanQuestionService;
    List<TJuanQuestionVo> voList = new ArrayList<>();
    List<String> questionCode = new ArrayList<>();
    private final String tkId;
    private final String klId;

    public TJunQuestionListener(String coursrId,String knowledgeId) {
        this.itJuanQuestionService = SpringUtils.getBean(ITJuanQuestionService.class);
        this.tkId = coursrId;
        this.klId = knowledgeId;

    }

    @Override
    public void invoke(TJuanQuestionVo vo, AnalysisContext context) {
        //非空校验
//        ValidatorUtils.validate(vo);
        //模板内题目编码是否重复
//        if(!questionCode.contains(vo.getQuestionCode())){
//            voList.add(vo);
//            questionCode.add(vo.getQuestionCode());
//        }
        if(null == vo.getQuestionType()){
            vo.setQuestionType("1");
        }
        if(null == vo.getQuestionEasy()){
            vo.setQuestionEasy("1");
        }
        if(null == vo.getQuestionGrade()){
            vo.setQuestionGrade("1");
        }
        if(null == vo.getCapabilityLevel()){
            vo.setCapabilityLevel("1");
        }
        if(null == vo.getKnowledgeCode()){
            vo.setKnowledgeId(klId);
        }
        vo.setCourseId(tkId);
        voList.add(vo);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        itJuanQuestionService.importExcell(voList,tkId);
    }

    @Override
    public ExcelResult<TJuanQuestionVo> getExcelResult() {
        return null;
    }
}
